<?php


namespace app\model;

use think\facade\Db;
use think\Model;
use app\model\VehicleModel;
class CarModel extends Model
{
    protected $table = "store_car";

    /**
     * @param $id
     * @return CarModel|array|mixed|Model|null
     * @notes 车辆详情
     * @author: dongdong
     * @date: 2025/1/10 18:38
     */
    public function info($id){
        try {
            $VehicleModel = new VehicleModel();
            $find = self::where(["id"=>$id])->find();

            $find['driver'] = Db::table("store_driver")->field("id,title,title_ru,bind_user_id")->where(["id"=>$find['driver_id']])->find();

            $find['driver_title'] = '';
            $find['driver_title_ru'] = '';
            $find['bind_user_title'] = '';
            $find['bind_user_title_ru'] = '';

            if($find['driver']){
                $find['driver_title'] = $find['driver']['title'];
                $find['driver_title_ru'] = $find['driver']['title_ru'];
                $bind_user_id = $find['driver']['bind_user_id'];
                if($bind_user_id){
                    $bind_user = Db::connect('fast_mysql')->table('fa_user')->field("id,username,username_ru")->where(["id"=>$bind_user_id])->find();
                    if($bind_user){

                        $find['bind_user_title'] = $bind_user['username'];
                        $find['bind_user_title_ru'] = $bind_user['username_ru'];
                    }
                }
            }else{
                $bind_user = Db::connect('fast_mysql')->table('fa_user')->field("id,username,username_ru")->where(["id"=>$find['user_id']])->find();
                if($bind_user){
                    $find['bind_user_title'] = $bind_user['username'];
                    $find['bind_user_title_ru'] = $bind_user['username_ru'];
                }
            }

            $find['car_category_name'] = $VehicleModel->field("id,title,title_ru")->where(["id"=>$find['car_category_id']])->find();
            $find['area'] = Db::connect('fast_mysql')->table('fa_area')->where(["id"=>$find['current_city_id']])->find();
            return  $find;
        }catch (\Exception $e){
            return [];
        }
    }


    /**
     * @param $car_category_id
     * @return \app\model\VehicleModel|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 返回载具名称
     * @author: dongdong
     * @date: 2025/1/15 11:44
     */
    public function car_category_name($car_category_id){
        $VehicleModel = new VehicleModel();
        return $VehicleModel->field("id,title,title_ru")->where(["id"=>$car_category_id])->find();
    }

}